package com.ftg.selflearn.config;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author Guo 2022/8/7 0:07:02
 */
@Configuration
@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
public class swaggerConfig {

    @Bean
    public Docket createRestApi() {
        // 添加token参数
        RequestParameterBuilder requestParameterBuilder = new RequestParameterBuilder()
                .name("token")
                .description("token")
                .in(ParameterType.HEADER)
                // 类内部创建了默认的builder以供属性设置
                .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("1")
                        .allowEmptyValue(true));
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(Lists.newArrayList(requestParameterBuilder.build()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("guo")
//                .description("springboot | swagger")
//                .contact(new Contact("", "", ""))
//                .version("1.0.0")
                .build();
    }
}
